﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySSO.Core;

namespace MySSOClient
{
    public partial class About : System.Web.UI.Page
    {
        private string ssoSiteUrl = "http://localhost:20687/Authorize/Index";
        private string tokenurl = "http://localhost:20687/api/User";
        protected void Page_Load(object sender, EventArgs e)
        {
            UserSSoModel userSSoModel = null;
            if (!string.IsNullOrEmpty(Request.QueryString["Token"]))
            {
                userSSoModel = this.GetUserByToken(Request.QueryString["Token"].ToString());
                if (userSSoModel != null)
                {
                    Session["Token"] = Request.QueryString["Token"].ToString();
                    string url = Request.Url.AbsoluteUri;
                    url = HttpQueryStringHelper.RemoveParm(url, "Token");
                    url = HttpQueryStringHelper.RemoveParm(url, "RequestId");
                    Response.Redirect(url);
                    //Response.Write(userSSoModel.username + "认证成功");

                    return;
                }
            }

            if (Session["Token"] != null)
            {
                userSSoModel = this.GetUserByToken(Session["Token"].ToString());
                if (userSSoModel != null)
                {
                    Response.Write(userSSoModel.username + "认证成功");
                    return;
                }

            }

            //获取存储的token值
            //if token存在，http请求验证token，验证通过，从token获取uid

            //如果token不存在或者验证不通过，则重定向到sso登录页面

            string returnurl = HttpUtility.UrlEncode(Request.Url.AbsoluteUri);
            Response.Redirect(string.Format("{0}?{1}={2}", ssoSiteUrl, "ReturnUrl", returnurl));
            return;

        }

        private UserSSoModel GetUserByToken(string token)
        {
            string requesturl = tokenurl;
            int timeout = 60;
            JObject o = JObject.FromObject(new { Token = token });
            string requestdata = o.ToString();
            bool ispost = true;
            string msg;
            string result = HttpHelper.HttpPostWebRequest(requesturl, timeout, requestdata, ispost, out msg);
            if (string.IsNullOrWhiteSpace(msg))
            {
                try
                {
                    JObject oresult = JObject.Parse(result);
                    ResultBase<UserSSoModel> reponsemodel = oresult.ToObject<ResultBase<UserSSoModel>>();
                    if (reponsemodel.code == "0")
                    {

                        return reponsemodel.data;

                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
}